package controllers

import (
	"github.com/gin-gonic/gin"
	"fmt"
	"net/http"
	"time"
)
// 计算1000以内可以整出3和5的和
func Handle(c *gin.Context) {
	start_time := time.Now()
	// 并行计算
	resultChan := make(chan int,3)
	// 可以整出3的和
	go dealwith(1000, 3, resultChan)
	// 可以整出5的和
	go dealwith(1000, 5, resultChan)
	// 既可以整出5也可以整出3的和
	go dealwith(1000, 15, resultChan)
	sum3,sum5,sum15 := <-resultChan, <-resultChan, <-resultChan
	sum := sum3 + sum5 + sum15
	fmt.Println(sum3, sum5, sum15, sum)

	end_time := time.Now()
	fmt.Println(end_time.Sub(start_time))
	c.JSON(http.StatusOK, gin.H{
    	"errcode": 0,
		"errmsg": "success",
		"data":"",
    })	
    return
}

func dealwith(num int, divider int, resultChan chan int) {
	total := 0
	for i:=0; i<num; i++ {
		if i % divider == 0 {
			total+=i
		}
	}
	resultChan <-total
}

